package com.example.controller;

import com.example.entity.DogWalking;
import com.example.entity.ServiceApplication;
import com.example.entity.User;
import com.example.service.DogWalkingService;
import com.example.service.ServiceApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/dogwalking")
public class DogWalkingController {
    @Autowired
    private DogWalkingService dogWalkingService;
    @Autowired  // 添加这行
    private ServiceApplicationService applicationService;

    // 主页面 - 服务列表
    @GetMapping("/list")
    public String listServices(@RequestParam(required = false) String role,
                               HttpSession session, Model model) {
        User user = (User) session.getAttribute("login_user");
        if (user == null) {
            return "redirect:/user/login";
        }

        if ("provider".equals(role)) {
            List<DogWalking> services = dogWalkingService.getServicesByProvider(user.getUser_id());
            model.addAttribute("services", services);
            model.addAttribute("role", "provider");
        } else if ("requester".equals(role)) {
            List<DogWalking> services = dogWalkingService.getServicesByRequester(user.getUser_id());
            model.addAttribute("services", services);
            model.addAttribute("role", "requester");
        } else {
            List<DogWalking> services = dogWalkingService.getAvailableServices();
            model.addAttribute("services", services);
            model.addAttribute("role", "browse");
        }

        return "dogwalking/list";
    }

    // 发布服务表单页
    @GetMapping("/publish")
    public String showPublishForm(Model model, HttpSession session) {
        User user = (User) session.getAttribute("login_user");
        if (user == null) {
            return "redirect:/user/login";
        }

        // 检查用户是否已有审核通过的申请
        ServiceApplication application = applicationService.getLatestApprovedApplication(
                user.getUser_id(), "DOG_WALKING");

        if (application == null || !"APPROVED".equals(application.getStatus())) {
            model.addAttribute("needsApproval", true);
        }

        model.addAttribute("service", new DogWalking());
        return "dogwalking/publish";
    }

    @PostMapping("/publish")
    public String publishService(DogWalking service,
                                 HttpSession session,
                                 RedirectAttributes redirectAttributes) {
        User user = (User) session.getAttribute("login_user");
        if (user == null) {
            return "redirect:/user/login";
        }

        // 检查用户是否通过审核
        ServiceApplication application = applicationService.getLatestApprovedApplication(
                user.getUser_id(), "DOG_WALKING");

        if (application == null || !"APPROVED".equals(application.getStatus())) {
            redirectAttributes.addFlashAttribute("error", "请先提交并通过服务申请审核");
            return "redirect:/application/submit?type=DOG_WALKING";
        }

        service.setProvider_id(user.getUser_id());
        service.setApplication_id(application.getApplication_id());
        service.setStatus(0); // 0表示可预约
        dogWalkingService.createService(service);
        return "redirect:/dogwalking/list?role=provider";
    }

    // 预约服务详情页
    @GetMapping("/detail/{serviceId}")
    public String serviceDetail(@PathVariable int serviceId, Model model) {
        DogWalking service = dogWalkingService.getServiceById(serviceId);
        if (service == null) {
            return "redirect:/dogwalking/list";
        }
        model.addAttribute("service", service);
        return "dogwalking/detail";
    }

    // 处理预约请求
    @PostMapping("/request/{serviceId}")
    public String requestService(@PathVariable int serviceId, HttpSession session) {
        User user = (User) session.getAttribute("login_user");
        if (user == null) {
            return "redirect:/user/login";
        }

        dogWalkingService.requestService(serviceId, user.getUser_id());
        return "/dogwalking/list?role=requester";
    }


}